Skip to content

fix(typescript-fetch): add anyToJSON to runtime.ts#24216

Merged
wing328 merged 1 commit into
OpenAPITools:masterfrom
mstojanovic8:fix/typescript-fetch-anytojson-v2
Jul 7, 2026
Merged

fix(typescript-fetch): add anyToJSON to runtime.ts#24216
wing328 merged 1 commit into
OpenAPITools:masterfrom
mstojanovic8:fix/typescript-fetch-anytojson-v2

Conversation

@mstojanovic8

@mstojanovic8 mstojanovic8 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

The generated API code calls anyToJSON() for any-typed properties in form data (e.g. photo, file uploads), but the function was never defined in runtime.ts, causing a ReferenceError at runtime.

Fixes #1877

PR checklist

  • Read the contribution guidelines.
  • Regenerated typescript-fetch samples: ./bin/generate-samples.sh ./bin/configs/typescript-fetch*
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha @dennisameling


Summary by cubic

Adds a pass-through anyToJSON to the typescript-fetch runtime and uses it for free-form object fields in multipart form data to prevent a runtime ReferenceError. Fixes #1877.

  • Bug Fixes

    • Define anyToJSON(value: any): any in runtime.ts.
    • Update apis.mustache to call runtime.anyToJSON for free-form object fields; regenerate samples; add 3_0/typescript-fetch/any_type_property.json.
  • Dependencies

    • Update sample jaxrs-spec-enum pom.xml: upgrade maven-surefire-plugin/maven-failsafe-plugin to 3.5.6 and switch tests to JUnit 5 (org.junit.jupiter:junit-jupiter-engine 5.14.4).

Written for commit 37c949c. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 22 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/others/typescript-fetch/additional-properties-in-multipart-issue/runtime.ts">

<violation number="1" location="samples/client/others/typescript-fetch/additional-properties-in-multipart-issue/runtime.ts:371">
P1: The new `anyToJSON` helper prevents the reported `ReferenceError`, but its raw pass-through implementation silently mis-serializes object or array values when they are appended to `FormData`. `FormData.append` stringifies non-Blob values via `String(value)`, so an object becomes `"[object Object]"` instead of a JSON string, which can corrupt multipart payloads at runtime without throwing. Consider using `JSON.stringify` for non-Blob objects so the function matches its `*ToJSON` naming and contract, or handling primitives and Blobs explicitly to avoid this silent data loss.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

}

// Pass-through serializer for `any`-typed properties in form data. See #1877.
export function anyToJSON(value: any): any {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: The new anyToJSON helper prevents the reported ReferenceError, but its raw pass-through implementation silently mis-serializes object or array values when they are appended to FormData. FormData.append stringifies non-Blob values via String(value), so an object becomes "[object Object]" instead of a JSON string, which can corrupt multipart payloads at runtime without throwing. Consider using JSON.stringify for non-Blob objects so the function matches its *ToJSON naming and contract, or handling primitives and Blobs explicitly to avoid this silent data loss.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/others/typescript-fetch/additional-properties-in-multipart-issue/runtime.ts, line 371:

<comment>The new `anyToJSON` helper prevents the reported `ReferenceError`, but its raw pass-through implementation silently mis-serializes object or array values when they are appended to `FormData`. `FormData.append` stringifies non-Blob values via `String(value)`, so an object becomes `"[object Object]"` instead of a JSON string, which can corrupt multipart payloads at runtime without throwing. Consider using `JSON.stringify` for non-Blob objects so the function matches its `*ToJSON` naming and contract, or handling primitives and Blobs explicitly to avoid this silent data loss.</comment>

<file context>
@@ -367,6 +367,11 @@ export function mapValues(data: any, fn: (item: any) => any) {
 }
 
+// Pass-through serializer for `any`-typed properties in form data. See #1877.
+export function anyToJSON(value: any): any {
+    return value;
+}
</file context>
Suggested change
export function anyToJSON(value: any): any {
export function anyToJSON(value: any): any {
if (value instanceof Blob) {
return value;
}
if (typeof value === 'object' && value !== null) {
return JSON.stringify(value);
}
return value;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!
anyToJSON is always called wrapped in JSON.stringify(anyToJSON(value)) in the generated code, so a plain pass-through is correct — adding JSON.stringify inside would cause double serialization.

@wing328 wing328 added this to the 7.24.0 milestone Jul 6, 2026
@mstojanovic8 mstojanovic8 force-pushed the fix/typescript-fetch-anytojson-v2 branch from 377065a to d14bcb2 Compare July 6, 2026 17:41
@mstojanovic8

Copy link
Copy Markdown
Contributor Author

Looking at the failed actions

@mstojanovic8

Copy link
Copy Markdown
Contributor Author

The 'Samples TS clients' check appears to be a pre-existing failure on master (all recent runs are failing as far as I can see).
This PR did not introduce this issue imho.

  The generated API code calls anyToJSON() for any-typed properties in
  form data, but the function was never defined in runtime.ts, causing
  a ReferenceError at runtime.

  Fixes OpenAPITools#1877
@mstojanovic8 mstojanovic8 force-pushed the fix/typescript-fetch-anytojson-v2 branch from d14bcb2 to 37c949c Compare July 6, 2026 18:07
@wing328

wing328 commented Jul 7, 2026

Copy link
Copy Markdown
Member

agreed the TS Fetch sample failure is not caused by this PR

i'll try to fix the CI tests later this week

@wing328 wing328 merged commit 19c5943 into OpenAPITools:master Jul 7, 2026
54 of 55 checks passed
@wing328 wing328 mentioned this pull request Jul 7, 2026
3 tasks
@wing328

wing328 commented Jul 7, 2026

Copy link
Copy Markdown
Member

update: merged #24219 to fix the ts fetch tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][typescript-fetch] Schema with type object generates undefined function

2 participants